<template>
  <div>
    <el-dialog class="positionDialog" :title="'入场过磅'" :close-on-click-modal="false" :visible="showDialog" width="98%"
      :before-close="handleClose">
      <div class="content">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
          <div class="title fir-title">
            <svg-icon class="svg-icon" icon-class="infoAdd" />
            <span>入场过磅</span>
            <div class="button-box">
              <div :style="`background:${this.ruleForm.radio ? 'red' : '#0e7b67'}`" @click="radioClick"
                class="button-boxItem">作为新记录添加</div>
            </div>
          </div>
          <div class="form">
            <el-form-item class="activeClassInput" label="车牌号" prop="carNumber" style="width:500px">
              <el-input v-model="ruleForm.carNumber" placeholder="请输入车牌号" :disabled="true"></el-input>
              <el-button class="redAdd" style="border-color:green ;
                  color:green ;" @click="getCarName">刷新</el-button>
              <el-button class="redAdd" style="width:80px"
                @click="handleShowAddDialog('editCarNumber')">更正/录入</el-button>
            </el-form-item>
          </div>

          <div class="title">
            <span>车辆信息</span>
          </div>
          <div class="form">
            <el-form-item label="司机姓名" prop='driverPhone'>
              <el-select style="width: 100%;" v-model="ruleForm.driverPhone" filterable placeholder="请选择司机姓名或输入关键词"
                @change="handleDriverName">
                <el-option v-for="item, index in driverList" :key="item.value + index" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('driverInfo')">新增</el-button>
            </el-form-item>
            <el-form-item label="押车人姓名" prop="personPhone">
              <el-select style="width: 100%;" v-model="ruleForm.personPhone" filterable placeholder="请选择押车人姓名或输入关键词"
                @change="handleEscortPerson">
                <el-option v-for="item, index in escortPersonList" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('betcarInfo')">新增</el-button>
            </el-form-item>
          </div>
          <div class="title">
            <span>基本信息</span>
          </div>
          <div class="form">
            <el-form-item label="农场" prop="farmCode">
              <el-select style="width: 100%;" v-model="ruleForm.farmCode" disabled filterable placeholder="请选择农场或输入关键词"
                @change="handleSelectTeam">
                <el-option v-for="item in farmList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="生产队" prop="teamCode">
              <el-select style="width: 100%;" v-model="ruleForm.sourceTeam" filterable placeholder="请选择生产队或输入关键词"
                @change="handleChangeMassifNumberKong">
                <el-option v-for="item in teamList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="地块号" prop='massifNumber'>
              <el-select style="width: 100%;" v-model="ruleForm.massifNumber" filterable placeholder="请选择地块号或输入关键词"
                :disabled="ruleForm.teamCode ? false : true" @change="setmassifName">
                <el-option v-for="item, index in massifNumberList" :key="item.value + index" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="种类" prop='cropType'>
              <el-select style="width: 100%;" v-model="ruleForm.cropType" filterable placeholder="请选择种类或输入关键词"
                @change="handleChangeCropType2">
                <el-option v-for="item in cropinfoList" :key="item.value" :label="item.label" :value="item.label">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('cropInfo')">新增</el-button>
            </el-form-item>
            <el-form-item label="作物品种" prop='cropVarieties'>
              <el-select style="width: 100%;" v-model="ruleForm.cropVarieties" filterable placeholder="请选择作物品种或输入关键词">
                <el-option v-for="item, index in cropVarietiesList" :key="item.value + index" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('cropVariety')">新增</el-button>
            </el-form-item>
            <el-form-item label="类别" prop='cropCategory'>
              <el-select style="width: 100%;" v-model="ruleForm.cropCategory" filterable placeholder="请选择类别或输入关键词">
                <el-option v-for="item in categoryList" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
              <el-button class="redAdd" @click="handleShowAddDialog('cropType')">新增</el-button>
            </el-form-item>
            <el-form-item label="开票人" prop='biller'>
              <el-input v-model="ruleForm.biller" placeholder="请输入开票人"></el-input>
            </el-form-item>
          </div>
          <div class="title">
            <span>过磅信息</span>
            <el-checkbox style="margin-left:50px" @change="radioChange" v-model="ruleForm.radio"><span
                style="font-weight: 900;">作为新记录添加</span></el-checkbox>
          </div>
          <div class="form">
            <el-form-item label="毛重(kg)" prop="">
              <el-input class="activeClassInput" oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.gw"
                placeholder="请输入毛重" disabled></el-input>
              <el-button class="redAdd" style="border-color:green ;
                  color:green ;" @click="getTotalWeight">刷新</el-button>
            </el-form-item>
            <el-form-item label="皮重(kg)" prop="">
              <el-input class="activeClassInput" oninput="value=value.replace(/[^0-9.]/g,'')" v-model="ruleForm.tare"
                placeholder="请输入皮重" @blur="handleTARE" disabled></el-input>
            </el-form-item>
            <el-form-item label="净重(kg)" prop="">
              <el-input v-model="ruleForm.nw" placeholder="请输入净重" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="过毛时间" :prop="ruleForm.gw ? 'hairingTime' : ''">
              <el-date-picker style="width: 100%;" v-model="ruleForm.hairingTime" format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="请选择过毛时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="过皮时间" :prop="ruleForm.tare && ruleForm.tare != 0 ? 'peelingTime' : ''">
              <el-date-picker style="width: 100%;" v-model="ruleForm.peelingTime" format="yyyy-MM-dd HH:mm:ss"
                value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="请选择过皮时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="过磅人" prop="weighPeople">
              <el-input v-model="ruleForm.weighPeople" placeholder="请输入过磅人"></el-input>
            </el-form-item>
          </div>
          <div class="title">
            <span>扦样质检信息</span>
          </div>
          <div class="form">
            <el-form-item label="含水量(%)" prop="moistureContent">
              <el-input-number :precision="2" style='width:100%' :min="0" :step="0.01" :max="100"
                controls-position="right" oninput="value=value.replace(/[^0-9.]/g,'')"
                v-model="ruleForm.moistureContent" placeholder="请输入含水量" @change="handleGetWater"></el-input-number>
              <span v-if="showWater" class="tips">{{ water ? `当前作物标准含水量为 ${water} %` : '当前作物暂无标准含水量' }}</span>
            </el-form-item>
          </div>
          <div class="title">
            <span>实际重量</span>
          </div>
          <div class="form">
            <el-form-item label="水份扣重(kg)" prop='waterBuckle'>
              <el-input v-model="ruleForm.waterBuckle" placeholder="当前作物存在含水量标准时计算" :disabled="true"></el-input>
              <span class="tips">水份扣重=(实时水份含量-标净准含水量)*净重</span>
              <el-button class="redAdd" @click="handleShowAddDialog('waterStandard')">新增</el-button>
            </el-form-item>
            <el-form-item label="扣杂重(kg)" prop='deductBuckle'>
              <el-input @blur="setSoildFun" v-model="ruleForm.deductBuckle" placeholder="当前作物存在含水量标准时计算"
                :disabled="true"></el-input>
              <span class="tips">扣杂重=净重*标准扣杂百分值</span>
              <el-button class="redAdd" @click="handleShowAddDialog('impurityStandard')">新增</el-button>
            </el-form-item>
            <el-form-item label="实重(kg)" prop='solid'>
              <el-input v-model="ruleForm.solid" placeholder="得出水份扣重和扣杂重后计算" :disabled="true"></el-input>
              <span class="tips">实重=净重-水份扣重-扣杂重</span>
            </el-form-item>
          </div>
        </el-form>

      </div>
      <div class="dialog-footer333">
        <el-button class="submit" @click="handleSubmit">确 认</el-button>
        <el-button class="back" @click="handleClose">返 回</el-button>
      </div>
      <el-dialog width="30%" title="提示" :visible.sync="innerVisible" append-to-body :close-on-click-modal="false">
        <div style="height:100px;text-align: center;">
          您本次过磅重量与上次过磅重量相近，请确认是否关联上次过磅重量
        </div>
        <div class="dialog-footer333">
          <el-button class="submit" @click="() => { innerVisible = false; innerVisible2 = false }">确 认</el-button>
          <!-- <el-button class="back" @click="handleClose">返 回</el-button> -->
        </div>
      </el-dialog>
    </el-dialog>
    <!-- 新增车牌号 -->
    <CarInfo ref="CarInfo" :showDialog.sync="carInfo"></CarInfo>
    <!-- 司机信息 -->
    <DriverInfo ref="DriverInfo" :showDialog.sync="driverInfo" @handleRefresh="handleRefreshList('driverInfo')"></DriverInfo>
    <!-- 押车人信息 -->
    <BetcarInfo ref="BetcarInfo" :showDialog.sync="betcarInfo" @handleRefresh="handleRefreshList('betcarInfo')">
    </BetcarInfo>
    <!-- 种类 -->
    <CropInfo ref="CropInfo" :showDialog.sync="cropInfo" @handleRefresh="handleRefreshList('cropInfo')"></CropInfo>
    <!-- 作物品种 -->
    <CropVariety ref="CropVariety" :showDialog.sync="cropVariety" @handleRefresh="handleRefreshList('cropVariety')"></CropVariety>
    <!-- 作物类别 -->
    <CropType ref="CropType" :showDialog.sync="cropType" @handleRefresh="handleRefreshList('cropType')"></CropType>
    <!-- 水份扣重 -->
    <WaterStandard ref="WaterStandard" :showDialog.sync="waterStandard"></WaterStandard>
    <!-- 扣杂标准 -->
    <ImpurityStandard ref="ImpurityStandard" :showDialog.sync="impurityStandard"></ImpurityStandard>
    <HandlTable ref="handlTableAdd" :showDialog.sync="dialogVisible" :setTableData='addTableDate'></HandlTable>
  </div>
</template>

<script>
import '../../../inStorage/scss/style.scss'
import rules from '@/utils/rules'
import { getUserKey } from '@/utils/auth'
import { Message, MessageBox } from 'element-ui'
import { formatDate, filterSelect } from '@/utils/methods'
import { addVehicleInfo, getCropWeight, getCarLicense, getVehicleInfo } from '@/api/table'
import { selectApi, carinfoApi, driverInfoApi, escortPersonApi, cropinfoSelectApi, categorySelectApi } from '@/api/select'
import { getPlotUnitListApi, cropTypeApi, addApi, editApi, waterContentApi, deductStandardApi, getAdmissionByCarNumber, refreshWithApi } from '@/api/inStorage'
import { clientTableApi } from '@/api/info'
import CustomerInfo from '@/views/info/components/CustomerInfo/dialog/AddPopu.vue'
import CarInfo from '@/views/info/components/CarInfo/dialog/AddPopu.vue'
import DriverInfo from '@/views/info/components/DriverInfo/dialog/AddPopu.vue'
import TableCom from '@/components/TableCom'
import HandlTable from './editCarNumber.vue';
import { getInfo } from '@/utils/auth'
import BetcarInfo from '@/views/info/components/BetcarInfo/dialog/AddPopu.vue'
import CropInfo from '@/views/info/components/CropInfo/dialog/AddPopu.vue'
import CropVariety from '@/views/info/components/CropVariety/dialog/AddPopu.vue'
import CropType from '@/views/info/components/CropType/dialog/AddPopu.vue'
import WaterStandard from '@/views/info/components/WaterStandard/dialog/AddPopu.vue'
import ImpurityStandard from '@/views/info/components/ImpurityStandard/dialog/AddPopu.vue'
export default {
  props: ['showDialog'],
  components: {
    CarInfo,
    DriverInfo,
    CustomerInfo,
    TableCom,
    HandlTable,
    BetcarInfo,
    CropInfo,
    CropVariety,
    CropType,
    WaterStandard,
    ImpurityStandard
  },
  data() {
    return {
      ruleForm: {
        clientName: "",
        carName: '',
        carType: '',
        createUser: '',
        createTime: '',
        phone: "",
        // carNumber: "",
        massifNumber: "",
        weighPeople: JSON.parse(getInfo()).oguName,
        biller: JSON.parse(getInfo()).oguName,
        farmCode: this.$farmCode,
        teamCode: this.$teamCode,
        massifNumber: '',
        cropVarieties: "",
        // 车牌号
        carNumber: ''
      },
      ruleForm2: {

      },
      showWater: false,
      water: '',
      rules: {
        biller: [
          { required: true, message: '请输入开票人', trigger: 'blur' },
        ],
        driverPhone: [
          { required: true, message: '请选择司机姓名', trigger: 'blur' },
        ],
        farmCode: [
          { required: true, message: '请选择农场', trigger: 'blur' },
        ],
        teamCode: [
          { required: true, message: '请选择生产队', trigger: 'blur' },
        ],
        massifNumber: [
          { required: true, message: '请选择地块号', trigger: 'blur' },
        ],
        cropType: [
          { required: true, message: '请选择作物类别', trigger: 'blur' },
        ],
        cropVarieties: [
          { required: true, message: '请选择作物品种', trigger: 'blur' },
        ],
        cropCategory: [
          { required: true, message: '请选择类别', trigger: 'blur' },
        ],
        carNumber: [
          { required: true, message: '请输入车牌号', trigger: 'blur' },
        ]

      },
      carList: [],
      carInfo: false,
      driverInfo: false,
      customerInfo: false,
      dialogVisible: false,
      carTypeInfo: 0,
      driverList: [],
      // 押车人
      escortPersonList: [],
      // 农场
      farmList: [],
      // 生产队
      teamList: [],
      // 种植季
      seasonList: [],
      seasonYear: '',
      // 作物种类
      cropinfoList: [],
      // 作物品种
      cropVarietiesList: [],
      // 作物类别
      categoryList: [],
      // 地块号
      massifNumberList: [],
      carInfo: false,
      driverInfo: false,
      betcarInfo: false,
      cropInfo: false,
      cropVariety: false,
      cropType: false,
      waterStandard: false,
      impurityStandard: false,
      timeOutFun: null,
      vehicleInfoId: '',
      timeOutFun1: null,
      innerVisible2: true,
      innerVisible: false,
    };
  },
  methods: {
    radioChange() {
      if (this.ruleForm.radio) {
        this.ruleForm2 = this.ruleForm;
        this.ruleForm = {
          ...this.ruleForm,
          driverPhone: "",
          personPhone: "",
          sourceTeam: "",
          // seasonId:"",
          massifNumber: "",
          cropType: "",
          cropVarieties: "",
          cropCategory: "",
          tare: "",
          solid: "",
          moistureContent: "",
          nw: "",
          peelingTime: "",
          id: ''
        }
        // this.ruleForm.radio=!this.ruleForm.radio;
      } else {
        this.ruleForm = { ...this.ruleForm2 }
        this.ruleForm.radio = false;
      }
      this.setgetAdmissionBy()
    },
    radioClick() {
      this.ruleForm.radio = true;
      this.ruleForm2 = this.ruleForm;
      this.ruleForm = {
        ...this.ruleForm,
        driverPhone: "",
        personPhone: "",
        sourceTeam: "",
        // seasonId:"",
        massifNumber: "",
        cropType: "",
        cropVarieties: "",
        cropCategory: "",
        tare: "",
        peelingTime: "",
        solid: "",
        moistureContent: "",
        id: ''
      }
      this.setgetAdmissionBy()
    },
    addTableDate(data) {
      this.ruleForm.carNumberBak = this.ruleForm.carNumber
      this.ruleForm.carNumber = data;
      this.innerVisible2 = true
      this.ruleForm = {
        ...this.ruleForm,
        driverPhone: "",
        personPhone: "",
        sourceTeam: "",
        // seasonId:"",
        massifNumber: "",
        cropType: "",
        cropVarieties: "",
        cropCategory: "",
        peelingTime: "",
        tare: "",
        solid: "",
        moistureContent: "",
        nw: "",
        id: ''
      }
      this.setgetAdmissionBy('cass')
      clearTimeout(this.timeOutFun1)
    },
    // 获取下拉框
    handleGetSelect() {
      Promise.all([
        carinfoApi(this.$farmCode),
        driverInfoApi(this.$farmCode),
        escortPersonApi(this.$farmCode),
        selectApi({ type: 'farm_type' }),
        cropinfoSelectApi(),
        cropinfoSelectApi(),
        categorySelectApi()
      ]).then(resList => {
        this.carList = filterSelect(resList[0].data)
        this.driverList = resList[1].data
        this.escortPersonList = resList[2].data
        if (this.$status != '0') {
          this.ruleForm.farmCode = this.$farmCode
          this.farmList = resList[3].data.dictItemList.filter(item => item.value == this.$farmCode)

          this.handleSelectTeam(this.$farmCode)
        } else {
          this.farmList = resList[3].data.dictItemList
        }
        // this.seasonList = resList[4].data.data.map(item => {
        //   return {
        //     label: item.name,
        //     value: item.id
        //   }
        // })
        this.ruleForm.seasonId = '7'
        // this.ruleForm.seasonId = this.seasonList[0].value
        this.cropinfoList = resList[5].data
        this.categoryList = filterSelect(resList[6].data)

        this.handleChangeMassifNumber()
        this.handleChangeCropType(this.ruleForm.cropType)
        // this.handleGetWater()

        // this.$refs.ruleForm.resetFields();
      })
    },
    // 获取填报人时间
    // handleGetInfo() {
    //   this.ruleForm.createUser = getUserKey()
    //   this.ruleForm.createTime = formatDate(new Date())
    // },
    setmassifName(e) {
      // this.ruleForm.massifName=
      let i = this.massifNumberList.findIndex(item => item.value == e)
      this.ruleForm.massifName = this.massifNumberList[i].label
    },
    // 选择生产队
    async handleSelectTeam(e) {
      // let i = this.farmList.findIndex(item => item.value == e)
      this.ruleForm.remarks = this.farmList[0].remarks;
      let res = await selectApi({ type: e })
      this.teamList = res.data.dictItemList
    },
    // 选择地块号
    async handleChangeMassifNumber() {
      let res = await getPlotUnitListApi({ teamId: this.ruleForm.teamCode })
      this.massifNumberList = res.data.map(item => {
        return {
          label: item.massifName,
          value: item.massifNumber
        }
      })
    },
    // 选择地块号
    async handleChangeMassifNumberKong() {
      this.$set(this.ruleForm, 'massifNumber', '')
      this.massifNumberList = []
      let res = await getPlotUnitListApi({ teamId: this.ruleForm.teamCode })
      this.massifNumberList = res.data.map(item => {
        return {
          label: item.massifName,
          value: item.massifNumber
        }
      })
    },
    // 选择种类
    async handleChangeCropType(e) {
      if (!e) return
      this.handleGetWater()
      // this.ruleForm.gw = ''
      // this.ruleForm.tare = ''
      // this.ruleForm.nw = ''
      // this.ruleForm.moistureContent = ''
      // this.ruleForm.waterBuckle = ''
      // this.ruleForm.deductBuckle = ''
      // this.ruleForm.cropVarieties = ''
      let res = await cropTypeApi(e)
      this.cropVarietiesList = filterSelect(res.data)
    },
    async handleChangeCropType2(e) {
      if (!e) return
      this.handleGetWater()
      // this.ruleForm.gw = ''
      // this.ruleForm.tare = ''
      // this.ruleForm.nw = ''
      // this.ruleForm.moistureContent = ''
      // this.ruleForm.waterBuckle = ''
      // this.ruleForm.deductBuckle = ''
      let res = await cropTypeApi(e)
      this.cropVarietiesList = filterSelect(res.data)
    },
    // 皮重
    handleTARE() {
      // totalWeight tareWeight netWeight
      if (this.ruleForm.tare) {
        this.ruleForm.tare = (this.ruleForm.tare * 1).toFixed(2)
        this.ruleForm.nw = (this.ruleForm.gw - this.ruleForm.tare).toFixed(2)
      }

    },
    // 含水量
    async handleGetWater() {
      this.showWater = true
      if (this.ruleForm.moistureContent) {
        this.ruleForm.moistureContent = (this.ruleForm.moistureContent * 1)
        if (this.seasonYear && this.ruleForm.farmCode && this.ruleForm.teamCode && this.ruleForm.cropType && this.ruleForm.nw) {
          deductStandardApi({ year: this.seasonYear, farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropName: this.ruleForm.cropType }).then(({ data }) => {

            // this.ruleForm.deductBuckle = (this.ruleForm.nw * (data / 100)).toFixed(2)
            this.$set(this.ruleForm, 'deductBuckle', (this.ruleForm.nw * (data / 100)).toFixed(2));
          })
          waterContentApi({ year: this.seasonYear, farmCode: this.ruleForm.farmCode, teamCode: this.ruleForm.teamCode, cropName: this.ruleForm.cropType }).then(({ data }) => {
            this.water = data
            if (this.ruleForm.moistureContent >= data) {
              this.$set(this.ruleForm, 'waterBuckle', ((this.ruleForm.nw * (this.ruleForm.moistureContent - data)) / 100).toFixed(2));
              this.$set(this.ruleForm, 'solid', (this.ruleForm.nw * 1) - (((this.ruleForm.nw * (this.ruleForm.moistureContent - data)) / 100) + (this.ruleForm.deductBuckle * 1)));
            } else {
              Message({
                message: '当前含水量小于标准含水量，将不予计算',
                type: 'warning',
                duration: 3 * 1000
              })
              this.$set(this.ruleForm, 'waterBuckle', '');
              this.$set(this.ruleForm, 'solid', '');
            }
          })
        }
      } else {
        this.$set(this.ruleForm, 'waterBuckle', '');
        this.$set(this.ruleForm, 'deductBuckle', '');
        this.$set(this.ruleForm, 'solid', '');
      }
    },
    // 选择司机
    handleDriverName(e) {
      let i = this.driverList.findIndex(item => item.value == e)
      this.ruleForm.driverName = this.driverList[i].label
    },
    // 选择司机 escortPersonList
    handleEscortPerson(e) {
      let i = this.escortPersonList.findIndex(item => item.value == e)
      this.ruleForm.personName = this.escortPersonList[i].label
    },
    handleClose() {
      this.$emit('update:showDialog', false)
      this.ruleForm = {
        farmCode: this.$farmCode,
        teamCode: this.$teamCode,
        radio: false,
        weighPeople: JSON.parse(getInfo()).oguName,
        biller: JSON.parse(getInfo()).oguName,
        carNumber: ''
      };
      this.carList = [];
      this.driverList = [];
      this.escortPersonList = [];
      this.farmList = [];
      this.teamList = [];
      this.seasonList = [];
      this.cropinfoList = [];
      this.cropVarietiesList = [];
      this.categoryList = [];
      this.massifNumberList = [];
      this.$refs.ruleForm.resetFields();
      clearTimeout(this.timeOutFun)
      clearTimeout(this.timeOutFun1)
      this.water = ''
      this.showWater = false
      this.innerVisible2 = true
      this.$emit('getCarNumber')
    },
    setSoildFun() {
      // this.ruleForm={
      //   ...this.ruleForm,
      //   solid:this.ruleForm.nw - this.ruleForm.waterBuckle - this.ruleForm.deductBuckle,
      // }
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.tare == 0) {
            // vehicleInfoId
            addApi({ ...this.ruleForm, nw: "",modifyTheID :"1" }).then((res) => {
              this.handleClose()
            })
            // addVehicleInfo(this.ruleForm).then((res)=>{
            //   this.handleClose();
            //   this.$emit('handleRefresh')
            // })
            this.$emit('handleRefresh')
          } else {
            if (this.ruleForm.radio) {
              addApi({ ...this.ruleForm, id: '', nw: "",modifyTheID :"1" }).then((res) => {
                this.handleClose()
              })
              this.$emit('handleRefresh')
            } else {
              editApi({ ...this.ruleForm,modifyTheID :"1" }).then((res) => {
                this.handleClose()
              })
              this.$emit('handleRefresh')
            }

          }
        } else {
          return false;
        }
      });
    },
    async handleCropTypeEdit(e) {
      let res = await cropTypeApi(e)
      this.cropVarietiesList = filterSelect(res.data)
    },
    // 新增弹框
    handleShowAddDialog(val) {
      if (val === 'CustomerInfo') {
        this.$refs.CustomerInfo.handleGetInfo()
        this.customerInfo = true
      }
      if (val === 'carInfo') {
        this.$refs.CarInfo.handleGetInfo()
        this.$refs.CarInfo.handleGetSelect()
        this.carInfo = true
      }
      if (val === 'driverInfo') {
        this.$refs.DriverInfo.handleGetInfo()
        this.$refs.DriverInfo.handleGetSelect()
        this.driverInfo = true
      }
      if (val === 'cropInfo') {
        this.$refs.CropInfo.handleGetInfo()
        this.cropInfo = true
      }
      if (val === 'betcarInfo') {
        this.$refs.BetcarInfo.handleGetInfo()
        this.betcarInfo = true
      }
      if (val === 'cropType') {
        this.$refs.CropType.handleGetInfo()
        this.cropType = true
      }
      if (val === 'cropVariety') {
        this.$refs.CropVariety.handleGetInfo()
        this.$refs.CropVariety.handleGetSelect()
        this.cropVariety = true
      }
      if (val == 'editCarNumber') {
        this.$refs.handlTableAdd.titleText = this.ruleForm.carNumberAA
        this.dialogVisible = true
      }
      if (val === 'waterStandard') {
        this.$refs.WaterStandard.handleGetInfo()
        this.$refs.WaterStandard.handleGetFarm()
        this.$refs.WaterStandard.handleGetSelect()
        this.waterStandard = true
      }
      if (val === 'impurityStandard') {
        this.$refs.ImpurityStandard.handleGetInfo()
        this.$refs.ImpurityStandard.handleGetFarm()
        this.$refs.ImpurityStandard.handleGetSelect()
        this.impurityStandard = true
      }
    },
  // 更新下拉框
  async handleRefreshList(val) {
      if (val === 'carInfo') {
        let res = await carinfoApi(this.$farmCode)
        this.carList = filterSelect(res.data)
      }
      if (val === 'driverInfo') {
        let res = await driverInfoApi(this.$farmCode)
        this.driverList = res.data
      }
      if (val === 'betcarInfo') {
        let res = await escortPersonApi(this.$farmCode)
        this.escortPersonList = res.data
      }
      if (val === 'cropInfo') {
        let res = await cropinfoSelectApi()
        this.cropinfoList = res.data
      }
      if (val === 'cropVariety') {
        if (!this.ruleForm.cropType) return
        let res = await cropTypeApi(this.ruleForm.cropType)
        this.cropVarietiesList = filterSelect(res.data)
      }
      if (val === 'cropType') {
        let res = await categorySelectApi()
        this.categoryList = filterSelect(res.data)
      }
      if (val === 'waterStandard') {

      }
      if (val === 'impurityStandard') {
      }
    },
    // 获取毛重
    getZl() {
      clearTimeout(this.timeOutFun)
      getCropWeight({farmCode: this.$farmCode,teamCode:this.$teamCode?this.$teamCode:''}).then((res) => {
        this.ruleForm.gw = res.data.data
        this.timeOutFun = setTimeout(() => {
          this.getZl()
        }, 3000)
      })
    },
    getZl2() {
      clearTimeout(this.timeOutFun)
      getCropWeight({farmCode: this.$farmCode,teamCode:this.$teamCode?this.$teamCode:''}).then((res) => {
        this.ruleForm.tare = res.data.data
        this.ruleForm = {
          ...this.ruleForm,
          tare: res.data.data,
          nw: this.ruleForm.gw * 1 - res.data.data
        }
        // this.ruleForm.nw=this.ruleForm.gw*1-this.ruleForm.tare*1
        this.timeOutFun = setTimeout(() => {
          this.getZl2()
        }, 3000)
      })
    },
    getTotalWeight() {
      getCropWeight({farmCode: this.$farmCode,teamCode:this.$teamCode?this.$teamCode:''}).then((res) => {
        this.ruleForm.gw = res.data.data;
        this.ruleForm = {
          ...this.ruleForm,
          gw: res.data.data
        }
      })
    },
    setgetAdmissionBy(aa) {
      getAdmissionByCarNumber({ carNumber: this.ruleForm.carNumber, farmCode: this.$farmCode }).then((ress) => {

        if (ress.data === null) {
          this.ruleForm = {
            ...this.ruleForm,
            tare: '0',
            // nw:this.ruleForm.gw,
            hairingTime: formatDate(new Date())
          }
          this.getZl()
        } else {
          clearTimeout(this.timeOutFun)
          if (this.ruleForm.radio) {
            this.getZl()
            this.ruleForm = {
              ...this.ruleForm,
              tare: '0',
              // nw:this.ruleForm.gw,
              hairingTime: formatDate(new Date())
            }
          } else {
            this.getZl2()

            if (!this.ruleForm.tare) {
              setTimeout(() => {
                this.mappingRuleForm(ress.data)
              })
            }
            if (aa) {
              setTimeout(() => {
                this.mappingRuleForm(ress.data)
              })
            }
            this.ruleForm = {
              ...this.ruleForm,
              // ...obj,
              nw: ress.data.gw * 1 - this.ruleForm.gw * 1,
            };
            if (this.innerVisible2 && this.ruleForm.nw < 500 && this.ruleForm.tare * 1 != 0) {
              this.innerVisible = true
            }
          }
        }
      })
    },
    getCarNumber() {
      clearTimeout(this.timeOutFun1)
      getCarLicense({farmCode:this.$farmCode,teamCode:this.$teamCode?this.$teamCode:''}).then((res) => {
        if (res.data.data == '') {
          this.timeOutFun1 = setTimeout(() => {
            this.getCarNumber()
          }, 3000)
        } else {
          // this.ruleForm.carNumber=res.data.data
          this.ruleForm = {
            ...this.ruleForm,
            carNumber: res.data.data
          }
          this.ruleForm.carNumberAA = res.data.data
          this.setgetAdmissionBy()
        }
      })

    },
    // 映射之前数据
    async mappingRuleForm(obj) {
      this.ruleForm = {
        ...this.ruleForm,
        ...obj,
        // gw:this.ruleForm.gw,
        gw: obj.gw,
        nw: obj.gw * 1 - this.ruleForm.gw * 1,
        peelingTime: formatDate(new Date()),
      };
      this.handleChangeMassifNumber()
      this.handleGetWater()
      this.handleChangeCropType(this.ruleForm.cropType)
    },
    getCarName() {
      // getCarLicense().then((res) => {
      //   this.ruleForm = {
      //     ...this.ruleForm,
      //     carNumber: res.data.data
      //   }
      //   this.innerVisible2 = true
      //   this.setgetAdmissionBy('cass')
      // })
      MessageBox.confirm('刷新后，将丢失本次车牌号记录，重新获取车牌号', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        cancelButtonClass: 'cancel',
        type: 'warning'
      }).then(() => {
        refreshWithApi({ carNumber: this.ruleForm.carNumber }).then(res => {
          this.ruleForm.carNumber = res.data.data
          if (res.data.data) {
            this.innerVisible2 = true
            this.setgetAdmissionBy('cass')
          }
        })
      })


    },
  },
  watch: {
    // 监听 obj.value 的变化
    'ruleForm.nw'(newVal, oldVal) {
      if (this.ruleForm.moistureContent && newVal && this.water) {
        // 水份扣重
        this.ruleForm.waterBuckle = (((newVal * 1) * ((this.ruleForm.moistureContent * 1) - (this.water * 1))) / 100).toFixed(2)
        // 实重
        this.ruleForm.solid = ((newVal * 1) - ((this.ruleForm.waterBuckle * 1) + (this.ruleForm.deductBuckle * 1))).toFixed(2)
        if ((this.ruleForm.moistureContent * 1) < (this.water * 1)) {
          Message({
            message: '当前含水量小于标准含水量，将不予计算',
            type: 'warning',
            duration: 3 * 1000
          })
          this.ruleForm.waterBuckle = ''
          this.ruleForm.solid = ''
        }
      }
      // 扣杂重
      if (newVal && this.sundries) {
        this.ruleForm.deductBuckle = ((newVal * 1) * (this.sundries * 1 / 100)).toFixed(2)
      } else {
        this.ruleForm.deductBuckle = ''
      }
      // 有水份扣重 没扣杂重
      if (newVal && this.ruleForm.waterBuckle && !this.ruleForm.deductBuckle) {
        this.ruleForm.solid = ((newVal * 1) - (this.ruleForm.waterBuckle * 1)).toFixed(2)
      }
      // 有扣杂重 没水份扣重
      if (newVal && !this.ruleForm.waterBuckle && this.ruleForm.deductBuckle) {
        this.ruleForm.solid = ((newVal * 1) - (this.ruleForm.deductBuckle * 1)).toFixed(2)
      }
      // 没扣重  实重=净重
      if (newVal && !this.ruleForm.waterBuckle && !this.ruleForm.deductBuckle) {
        this.ruleForm.solid = (newVal * 1).toFixed(2)
      }
    },
    'ruleForm.carNumber': (a, b) => {
      // console.log(a, b);
    }
  }
}
</script>

<style lang='scss' scoped>
.form {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}

.el-form-item {
  width: 33%;
  margin-bottom: 10px;
}

.content {
  display: flex;
  padding: 0;
}

::v-deep .widthLabel {
  .el-form-item__label {
    width: 160px !important;
  }
}

.fir-title {
  margin-top: 0 !important;
  display: flex;
  line-height: 55px;
}

.button-box {
  // background: #0e7b67;

  border-radius: 5px;
  margin: 0;
  padding: 0;
  margin-left: 1240px;
  line-height: 30px;

  .button-boxItem {
    background: #0e7b67;
    cursor: pointer;
    padding: 0 15px;
    height: 40px;
    line-height: 40px;
    border-radius: 20px;
    color: #fff;
  }
}

.inStoragePCButton {
  width: 90px;
  height: 55px;
  position: relative;
  float: left;
  background: #0e7b67;
  color: #fff;
  font-weight: 700;
  font-size: 1.2vw;
  line-height: 55px;
  text-align: center;
  // margin: 2vh 1vw;
  border-radius: 5px;
  cursor: pointer;
}

.inStoragePCButton2 {
  width: 90px;
  height: 55px;
  position: relative;
  float: left;
  background: pink;
  color: #fff;
  font-weight: 700;
  font-size: 1.2vw;
  line-height: 55px;
  text-align: center;
  // margin: 2vh 1vw;
  border-radius: 5px;
  cursor: pointer;
}


::v-deep .el-form-item__content {
  display: flex;
  align-items: center;
  height: 36px;
  line-height: 36px;
}

::v-deep .el-input {
  flex: 1;
  height: 36px;
}

::v-deep .el-select {
  flex: 1;
  height: 36px;
}

::v-deep .el-input__icon {
  line-height: 36px;
}

.tips {
  position: absolute;
  color: #666;
  top: 30px;
  font-size: 12px;
}

::v-deep .el-input__inner {
  // color: rgb(92, 112, 234) !important;
  height: 36px;
}

.activeClassInput {
  ::v-deep .el-input__inner {
    color: rgb(92, 112, 234) !important;
    height: 36px;
    font-weight: 900;
    font-size: 17px;
  }

  ::v-deep .el-input--suffix {
    font-size: 17px;
    color: rgb(92, 112, 234) !important;
    height: 36px;
    font-weight: 900;
  }

  ::v-deep .el-input {
    width: 100%;
  }
}

.title {
  color: #000;
  font-size: 17px !important;
  font-weight: bold;
  // margin-bottom: 0px;
  margin: 5px 0 !important;
}

.dialog-footer333 {
  position: absolute;
  width: 180px;
  left: 50%;
  bottom: 3%;
  transform: translateX(-50%);
}
</style>
